﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SPUtils;
using System.Reflection;

namespace RedisUtils
{
    public partial class Redis
    {
        public string DBRead_PureCompProps(SqlSugar.ISqlSugarClient cn, List<int> lstCompID, out Dictionary<int, string> dicIDPropJson)
        {
            string sErr = "";
            
            int iIDInDB;
            List<string[]> lstDSContent;
            List<int> lstIDInDB = new List<int>();
            dicIDPropJson = new Dictionary<int, string>();

            try
            {
                sErr = DBUtils.GetPureCompProps(cn, lstCompID, m_lstPureCompProp, out lstDSContent);
                if (sErr != "") goto Get_Out;

                for (int i = 0; i < lstDSContent.Count; i++)
                {
                    if (int.TryParse(lstDSContent[i][0], out iIDInDB) == false)
                    {
                        sErr = string.Format(FMTUtils.CannotConvertStringToInteger, lstDSContent[i][0]);
                        goto Get_Out;
                    }
                    lstIDInDB.Add(iIDInDB);

                    dicIDPropJson.Add(iIDInDB, "");
                }


                sErr = Assemble_Prop_Str(lstDSContent, lstIDInDB, PropCat.PureConstProp, ref dicIDPropJson);
                if (sErr != "") goto Get_Out;

                Console.WriteLine($" DB:\tTable `{SPTable.NIST_PureCompProps}` read successfully.");
            }
            catch (Exception ex)
            {
                sErr = ex.Message + "\r\n" + ex.StackTrace;
                goto Get_Out;
            }

        Get_Out:

            if (sErr != "")
            {
                sErr += "\r\nClass: " + MethodBase.GetCurrentMethod()?.DeclaringType + "  Method: " + MethodBase.GetCurrentMethod()?.Name;
            }

            return sErr;
        }

        public string DBRead_TDepProps(SqlSugar.ISqlSugarClient cn, List<int> lstCompID, Enums.PureTDepPropDataType xPropType, out Dictionary<int, string> dicIDPropJson)
        {
            string sErr = "";

            int iIDInDB;
            List<string[]> lstDSContent;
            List<int> lstIDInDB = new List<int>();
            dicIDPropJson = new Dictionary<int, string>();

            try
            {
                sErr = DBUtils.GetPureCompTDepProp(cn, lstCompID, xPropType, out lstDSContent);
                if (sErr != "") goto Get_Out;

                for (int i = 0; i < lstDSContent.Count; i++)
                {
                    if (int.TryParse(lstDSContent[i][0], out iIDInDB) == false)
                    {
                        sErr = string.Format(FMTUtils.CannotConvertStringToInteger, lstDSContent[i][0]);
                        goto Get_Out;
                    }
                    lstIDInDB.Add(iIDInDB);

                    dicIDPropJson.Add(iIDInDB, "");
                }

                sErr = Assemble_Prop_Str(lstDSContent, lstIDInDB, PropCat.PureTDepProp, ref dicIDPropJson);
                if (sErr != "") goto Get_Out;

                Console.WriteLine($" DB:\tTable of {xPropType.ToString()} read successfully.");
            }
            catch (Exception ex)
            {
                sErr = "PropType = " + xPropType.ToString() + "\n";
                sErr += ex.Message + "\r\n" + ex.StackTrace;
                goto Get_Out;
            }

        Get_Out:

            if (sErr != "")
            {
                sErr += "\r\nClass: " + MethodBase.GetCurrentMethod()?.DeclaringType + "  Method: " + MethodBase.GetCurrentMethod()?.Name;
            }

            return sErr;
        }

        public string DBRead_BinProps(SqlSugar.ISqlSugarClient cn, List<int> lstCompID, Enums.PropertyMethod xPropType, out Dictionary<string, string> dicIDPropJson)
        {
            string sErr = "";

            int iIDInDB_i;
            int iIDInDB_j;
            string sIDPair;
            List<string[]> lstDSContent;
            List<string> lstIDInDB = new List<string>();
            dicIDPropJson = new Dictionary<string, string>();

            try
            {
                sErr = DBUtils.GetBinaryProps(cn, lstCompID, xPropType, out lstDSContent);
                if (sErr != "") goto Get_Out;

                for (int i = 0; i < lstDSContent.Count; i++)
                {
                    if (int.TryParse(lstDSContent[i][0], out iIDInDB_i) == false)
                    {
                        sErr = string.Format(FMTUtils.CannotConvertStringToInteger, lstDSContent[i][0]);
                        goto Get_Out;
                    }
                    if (int.TryParse(lstDSContent[i][1], out iIDInDB_j) == false)
                    {
                        sErr = string.Format(FMTUtils.CannotConvertStringToInteger, lstDSContent[i][0]);
                        goto Get_Out;
                    }
                    sIDPair = iIDInDB_i.ToString() + "/" + iIDInDB_j.ToString();
                    lstIDInDB.Add(sIDPair);
                    dicIDPropJson.Add(sIDPair, "");
                }

                sErr = Assemble_Prop_Str(lstDSContent, lstIDInDB, PropCat.Binary, ref dicIDPropJson);
                if (sErr != "") goto Get_Out;

                Console.WriteLine($" DB:\tTable of {xPropType.ToString()} read successfully.");
            }
            catch (Exception ex)
            {
                sErr = "PropType = " + xPropType.ToString() + "\n";
                sErr += ex.Message + "\r\n" + ex.StackTrace;
                goto Get_Out;
            }

        Get_Out:

            if (sErr != "")
            {
                sErr += "\r\nClass: " + MethodBase.GetCurrentMethod()?.DeclaringType + "  Method: " + MethodBase.GetCurrentMethod()?.Name;
            }

            return sErr;
        }        
    }
}
